package ui;

import engine.Vector2D;
import java.awt.Color;
import java.awt.Graphics;
import ui.DrawingScreen.Bounds;

public abstract class GraphicObject {

    private static Integer nextAvailableId = 0;
    protected Integer id;
    protected Vector2D position;
    protected Vector2D speed;
    protected Color color;
    private Double coeffSpeed = 5.;

    public GraphicObject(Vector2D position, Vector2D speed, Color color) {
        this.position = position;
        this.speed = speed;
        this.color = color;

        //Identifiant unique
        this.id = nextAvailableId;
        nextAvailableId++;
    }

    abstract public void draw(Graphics g);

    public Vector2D getPosition() {
        return position;
    }

    public void setPosition(Vector2D position) {
        this.position = position;
    }

    public Vector2D getSpeed() {
        return speed;
    }

    public void setSpeed(Vector2D speed) {
        this.speed = speed;
    }

    //vrai si l'objet est dans les limites
    abstract public boolean isInsideBounds(Bounds bounds);

    //vrai si l'objet+vitesse est dans les limites 
    abstract public boolean isInsideBoundsNextStep(Bounds bounds, Vector2D maxMove);

    //Repositionne l'objet en additionnant le vecteur vitesse au vecteur position si l'objet est dans les limites
    //Rotate vers la direction desiree
    public void move(Bounds bounds) {
        Vector2D maxMove = new Vector2D(0.);
        if (isInsideBoundsNextStep(bounds, maxMove)) {//On peut effectuer le prochain déplacement
            this.position = Vector2D.add(this.position, Vector2D.mul(this.speed, coeffSpeed));
        } else if(maxMove.getX().intValue() > 0 || maxMove.getY().intValue() > 0){//Prochain déplacement trop grand, on effectue le déplacement max
            this.position = Vector2D.add(this.position, maxMove);
        }else{//Plus de déplacement possible

        }
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        GraphicObject other = (GraphicObject) obj;

        return other.id == this.id;
    }

    //Auto
    @Override
    public int hashCode() {
        int hash = 3;
        hash = 89 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }
    
    public void setCoeffSpeed(Double coeffSpeed) {
        this.coeffSpeed = coeffSpeed;
    }
}
